package patterns.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2016/12/1 0001.
 * subject 类，包含绑定观察者和通知观察者的方法
 */
public class Subject {
    private List<Observer> observers = new ArrayList<>();
    private int state;

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
        notifyAllObservers();
    }

    /**
     * 通知更新所有observer
     */
    private void notifyAllObservers() {
        for (Observer ob : observers) {
            ob.update();
        }
    }

    /**
     * 绑定observer观察者
     * @param observer
     */
    public void attach(Observer observer){
        observers.add(observer);
    }
}
